$(function () {


    get_add()
    // 1.渲染页面，获取传过来的文章内容
    function get_add() {
        // 先获取传过来的id字符串
        let sear = new URLSearchParams(window.location.search)
        // 截取id
        let id = sear.get('id')
        // console.log(id);
        $.ajax({
            type: 'get',
            url: '/index/article',
            data: { id },
            success: function (backData) {
                // console.log(backData);
                /*  let {title,state,read,next,id,state,read,next,id,date,cover,content,comments,categoryId,category,author} = backData.data */
                let back = backData.data
                let res = template('zidingyi', back)
                $('.ziding').html(res)
            }
        })
    }


    // 2.用户发表评论
    function get_pin() {
        // 先获取传过来的id字符串
        let sear = new URLSearchParams(window.location.search)
        // 截取id
        let id = sear.get('id')
        // console.log(id);

        $('.comment_sub').on('click',function(e){
            e.preventDefault()
            let name = $('.comment_name').val().trim()
            let date = $('.comment_input').val().trim()
            if(name != '' && date != ''){
                $.ajax({
                    type:'post',
                    url:'/index/post_comment',
                    data:{
                        author:name,
                        content:date,
                        articleId:id
                    },
                    success:function(backData){
                        layer.msg(backData.msg)
                        $('.comment_name').val('')
                        $('.comment_input').val('')
                        get_zui()
                    }
                })
            }
            
        })
    }
    get_pin()


    // 3.下面评论列表
    // 先获取传过来的id字符串
    let sear = new URLSearchParams(window.location.search)
    // 截取id
    let id = sear.get('id')
    $.ajax({
        type:'get',
        url:'/index/get_comment',
        data:{
            articleId:id
        },
        success:function(backData){
            console.log(backData);
            let res = template('user_pin',backData)
            $('.comment_list_con').html(res)
        }
    })


    // 4.最新评论
    function get_zui(){
        $.ajax({
            type:'get',
            url:'/index/latest_comment',
            success:function(backData){
                // console.log(backData);
                let res = template('pinglun',backData)
                $('.comment_list').html(res)
            }
        })
    }
    get_zui()

    // 5.一周热门排行
    $.ajax({
        type:'get',
        url:'/index/rank',
        success:function(backData){
            // console.log(backData);
            let res = template('paihang',backData)
            $('.paihang').html(res)
        }
    })


    // 6.首页焦点关注
    $.ajax({
        type:'get',
        url:'/index/attention',
        success:function(backData){
            console.log(backData);
            let res = template('jiaodian',backData)
            $('.guanzhu_list').html(res)
        }
    })


    // 7.全部分类（左右两边类别获取）
    $.ajax({
        type:'get',
        url: '/admin/category/list',
        success: function(backData) {
            // console.log(backData);
            let add_leibie = template('add_leibie',backData)
            $('.level_two').html(add_leibie)
            $('.left_menu').html(add_leibie)
        }
    })
})